package module::gucci_uk::parser;
use Encode;

# ---------------------------------------------
# Constructor function.
# Modify Log
# 12/04/2012 Alex create
# ---------------------------------------------
sub new {
	my $class = shift;
	my $self  = {};
	bless $self, $class;
	return $self;
}
# ---------------------------------------------
# parse web site
# 12/04/2012 Alex create
# ---------------------------------------------
sub parseAndgetInfo {
	my $this 	= shift;
	my $url  	= shift;
	my $hostUrl = shift;
	
	my $charset = 'utf-8';
	my $data = $main::filedata;
    
	my $content = $data->getPage( $url, $charset );
	# get page failed
	if ( !$content ) {
		# error 300001
		Log::Write::setLog("E300001",{'URL'=>$url});
		return;
	}

	# Get the ProductId	and the mix productID number.
	my $productId;
	if ( $url =~  /.*\/(.*?)$/is ){
		$productId = $1;
    }
	if ( !$productId ){
        # E300002 : Failed get XXX field for page XXX
		Log::Write::setLog('E300002',{'POINT'=>"ID",'URL'=>$url});
		return;
    }	
	
	# Get the name.
	my $name;
	my $colorNameAppend;
	if( $content =~ /pageName = "([^"]*?)";/is ){
	    $name = $1;
	    $name =~ s/^\s+|\s+$//g;
	    $name =~ s/&#40;/(/igs;
		$name =~ s/&#41;/)/igs;		
	}

	if ( !$name ){
        # E300002 : Failed get XXX field for page XXX
		Log::Write::setLog('E300002',{'POINT'=>"Name",'URL'=>$url});
		return;
    }
    
	# Get the size and color.
	my @colorArray;
	my @sizeArray;
	my @colorCheckArray;
	my @color_ID;
	my %color_rep;
	if ( $content =~ /<ul class="items">(.*?)<\/ul>/is ){
		my $color_content = $1;
		my $colorcount = 1;
		while ( $color_content =~ /(<li[^>]*>.*?)<\/li>/isg ){
			my $colortemp_content = $1;
			if ( $colortemp_content =~ /<li [^>]*?title="([^"]*?)".*?<a href="([^"]*?)".*?<img alt="[^>]*?src="([^"]*?)"/is ){
				my $color_name = $1;
				my $color_url = $2;
				my $color_swatch  = $3;
				$color_name =~ s/\n//isg;
				my %color;
				my $color_img;				
				if ( $color_swatch ){
					$color_img = $color_swatch;
					$color_img =~ s/web_variation/web_full/isg;
				}
				
				if ( $color_name ){
					my $colorrep_name = $color_name;
					if ( $color_rep{$colorrep_name} ){
						if(length($color_name) > 39 ){
							$color_name = substr($color_name, 0, 36)."$colorcount...";
						}else{
							$color_name = $color_name."$colorcount";
						}
						$colorcount++;						
					}
					$color_rep{$colorrep_name} = 1;
					
					if(length($color_name) > 40 ){
						$color_name = substr($color_name, 0, 37).'...';
						if ( $color_rep{$color_name} ){
							$color_name =~ s/....$/$colorcount.../isg;
							$colorcount++;
						}else{
							$color_rep{$color_name} = 1;
						}
					}

					$color{"Name"} = $color_name;
					$color{"ImageURL"} = $color_img;
					$color{"SwatchURL"} = $color_swatch;
					push @colorArray, \%color;
										
					my $color_ID = $color_url;
					$color_ID =~ s/.*\/([^\/]*?)$/$1/isg;
					if ( $productId >= $color_ID ){
						$productId = $color_ID;
						$url = 'http://www.gucci.com'.$color_url;
						$imageURL = $color_img;
					}
					push @color_ID, $color_ID;					
				}
				elsif ( $colortemp_content =~ /rel="[^"]*?\*([^>]*?)\*/is ){
					my $color_name = $1;
					$color_name =~ s/^\s*|\s*$//isg;
					$color_name =~ s/\n//isg;
					
					my $colorrep_name = $color_name;
					if ( $color_rep{$colorrep_name} ){
						if(length($color_name) > 39 ){
							$color_name = substr($color_name, 0, 36)."$colorcount...";
						}else{
							$color_name = $color_name."$colorcount";
						}		
						$colorcount++;
					}
					$color_rep{$colorrep_name} = 1;
					
					if(length($color_name) > 40 ){
						$color_name = substr($color_name, 0, 37).'...';
						if ( $color_rep{$color_name} ){
							$color_name =~ s/....$/$colorcount.../isg;
							$colorcount++;
						}else{
							$color_rep{$color_name} = 1;
						}
					}
					$color{"Name"} = $color_name;
					$color{"ImageURL"} = $color_img;
					$color{"SwatchURL"} = $color_swatch;
					push @colorArray, \%color;
										
					my $color_ID = $color_url;
					$color_ID =~ s/.*\/([^\/]*?)$/$1/isg;
					if ( $productId >= $color_ID ){
						$productId = $color_ID;
						$url = 'http://www.gucci.com'.$color_url;
						$imageURL = $color_img;
					}
					push @color_ID, $color_ID;
				}
				$name =~ s/\Q$color_name\E//isg;							
			}			
		}
	}	
	
	
	
	my @priceArray;
	my $productCheckString;
	foreach my $color_ID ( @color_ID ){
		$productCheckString = $productCheckString.$color_ID."<split>";
		
		my $size_content_url = "http://www.gucci.com/uk/styles/$color_ID/2/populate_dynamic_content";
		my $size_content = $data->getPage( $size_content_url, $charset ); 
		while ( $size_content =~ /"buyable_status":([^"]*?),[^}]*?"size":"([^"]*?)"/isg ){
			my $sizerep = $1;
			my $size = $2;
			if ( $sizerep =~ /true/is ){
				push @sizeArray, $size;
			}
		}
		if ( $size_content =~ /"price":"([^"]*?)"/is ){
			my $tempprice = $1;
			$tempprice =~ s/\\u00a3//isg;
			push @priceArray, $tempprice;			
		}	
	}
	@sizeArray = &getUniqueItem(@sizeArray);

	# Get description
	my $description;
	my $des_content = $data->getPage( $url, $charset );
    if( $des_content =~ /<div id="description"[^>]*?>\s*<div>(.*?)<\/div>/is ){
    	$description = $1;
    	$description =~ s/<li><\/li>//isg;
    }
    if( $description !~ /\w/is ){
    	$description = $name;
    }

    # Get price
	my $price;
	my $currency = decode('utf8','£'); 
    if( @priceArray ){
        my @sortPriceArray = &sortArray(@priceArray);
        if( scalar(@sortPriceArray) <= 1 ){
            $price = $currency.$sortPriceArray[0];
        }else{
            $price = $currency.$sortPriceArray[0].'-'.$currency.$sortPriceArray[$#sortPriceArray];
        }
    }else{
        # E300002 : Failed get XXX field for page XXX
		Log::Write::setLog('E300002',{'POINT'=>"Price",'URL'=>$url});
		return;   	    	
    }
     
    if( !$imageURL ){
	    # E300002：Failed get XXX field for page XXX
		Log::Write::setLog("E300002",{'URL'=>$url,'POINT'=>'imageURL'});
		return;		   	
    }

	my %productInfoHash   = (
		"productCheckString" => $productCheckString,
		"ProductId"   => $productId,
		"ProductURL"  => $url,
		"Name"        => $name,
		"Price"       => $price,
		"SalePrice"   => $salePrice,
		"Description" => $description,
		"ImageURL"    => $imageURL,
		"Size"        => \@sizeArray,
		"Color"       => \@colorArray,
		"Time"        => Date::Time::getGMT());
	return %productInfoHash;
}

# ---------------------------------------------
# sort array
# Modify Log
# 12/06/2012 Alex create
# ---------------------------------------------

sub sortArray {
    my @noSortArray = @_;
    my %getNumberHash;
    my @sortArray;   
    foreach my $noSortArray(@noSortArray){
    	my @number = $noSortArray =~ m/(\d+)/g;
    	my $getNumber;
    	for(my $i = 0;$i <= $#number;$i++ ){
    		$getNumber = $getNumber.$number[$i];
    		
    	}
    	$getNumberHash{$noSortArray} = $getNumber;
    }   
    foreach my $key (sort {$getNumberHash{$a} <=> $getNumberHash{$b}} keys %getNumberHash){
    	push @sortArray,$key;
    }
    return @sortArray;
}

sub getUniqueItem {
    my @all = @_;
    my @only;
    my %count;
    @only = grep { ++$count{$_} < 2 } @all;
    return @only;
}
1;
